<?php

class Merit_pay_ratio extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Merit_pay_ratio':
	 * @var integer $id
	 * @var string $plate_no
	 * @var string $type
	 * @var integer $fk_truck_id
	 * @var integer $fk_type_id
	 * @var string $kind
	 * @var string $level
	 * @var double $profit_min
	 * @var double $profit_max
	 * @var double $ratio
	 * @var integer $state
	 * @var string $create_time
	 */
	
	public $type;
	public $plate_no;
	private $_kind=array('0'=>'A类','1'=>'B类','2'=>'C类','3'=>'D类','4'=>'E类');
	//private $_level=array('0'=>'A档','1'=>'B档','2'=>'C档','3'=>'D档');
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'merit_pay_ratio';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('type','length','max'=>20),
			array('kind','length','max'=>10),
			//array('level','length','max'=>10),
			array('a_profit_min, a_profit_max,b_profit_min, b_profit_max,c_profit_min, c_profit_max,d_profit_min, d_profit_max', 'numerical'),
			array('a_ratio,b_ratio,c_ratio,d_ratio', 'numerical','integerOnly'=>true,'min'=>0,'max'=>100),
			array('plate_no,type,kind,a_profit_min,a_profit_max,a_ratio,b_profit_min,b_profit_max,b_ratio,c_profit_min,c_profit_max,c_ratio,d_profit_min,d_profit_max,d_ratio,effect_date,lapse_date', 'required'),
			array('a_profit_min, a_profit_max,b_profit_min, b_profit_max,c_profit_min, c_profit_max,d_profit_min, d_profit_max', 'match' ,'pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('effect_date,lapse_date', 'match','pattern' => '/\d{4}-\d{2}$/','message'=>'{attribute} 必须为日期型(年-月)'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)//在验证之前将无需用户输入的字段写入
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()) return;
		$this->a_ratio=$this->a_ratio/100;
		$this->b_ratio=$this->b_ratio/100;
		$this->c_ratio=$this->c_ratio/100;
		$this->d_ratio=$this->d_ratio/100;
		$this->effect_date.='-01';
		$this->lapse_date.='-01';
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
	
	public function getKindOptions(){
		return $this->_kind;
	}
	
	public function getKindName($n){
		return $this->_kind[$n];
	}
	/**
	public function getLevelOptions(){
		return $this->_level;
	}
	
	public function getLevelName($n){
		return $this->_level[$n];
	}
	**/
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'plate_no' => '车牌号',
			'type' => '车型',
			'fk_truck_id' => '车辆id',
			'fk_type_id' =>'车型id',
			'kind' => '类别',
			//'level' => '分档',
			//'profit_min' => '利润下限(元)',
			//'profit_max' => '利润上限(元)',
			'a_profit_min' => 'A档利润下限(元)',
			'a_profit_max' => 'A档利润上限(元)',
			'a_ratio' => 'A档系数(%)',
			'b_profit_min' => 'B档利润下限(元)',
			'b_profit_max' => 'B档利润上限(元)',
			'b_ratio' => 'B档系数(%)',
			'c_profit_min' => 'C档利润下限(元)',
			'c_profit_max' => 'C档利润上限(元)',
			'c_ratio' => 'C档系数(%)',
			'd_profit_min' => 'D档利润下限(元)',
			'd_profit_max' => 'D档利润上限(元)',
			'd_ratio' => 'D档系数(%)',		
			//'ratio' => '核算系数(%)',
			'effect_date' => '生效日期',
			'lapse_date' => '失效日期',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='plate_no';
		$params[]='type';
		return $params;
	}
}